package org.codebistro.util.servlet;

/*
 * Copyright (C) 2010 by CODEBISTRO LLC.
 *
 * The license is granted to use, reproduce, modify, display, perform, sublicense
 * and distribute this code under the conditions of the Mozilla Public License v 1.1.
 *
 * The text of the license is available with this source as MPL-1.1.txt
 * or at http://www.mozilla.org/MPL/MPL-1.1.txt.
 */

import org.codebistro.util.Bark;
import org.codebistro.util.Strings;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.zip.GZIPInputStream;

public class HttpUtils {
	public static boolean isDefaultPort(int port, String scheme) {
		if ("http".equalsIgnoreCase(scheme))
			return port == 80;
		else if ("https".equalsIgnoreCase(scheme))
			return port == 443;
		else
			return false;
	}

    public static int getDefaultPort(String scheme) {
        if (scheme.equals("http")) {
            return 80;
        }
        if (scheme.equals("https")) {
            return 443;
        }
        throw new IllegalArgumentException("Illegal request scheme");
    }

    private static StringBuilder getHostURL(final HttpServletRequest r, StringBuilder result) {
        String scheme = r.getHeader("X-Forwarded-Scheme");
        if (Strings.isNullOrEmpty(scheme))
            scheme= r.getScheme();
        int port = !Strings.isNullOrEmpty(r.getHeader("X-Forwarded-Scheme")) ? getDefaultPort(scheme) :
                r.getServerPort();
        result.append(scheme);
        result.append("://").append(r.getServerName());
        if (!isDefaultPort(port, scheme))
            result.append(":").append(port);
        return result;
    }

    public static String getHostURL(final HttpServletRequest r) {
        return getHostURL(r, new StringBuilder()).toString();
    }

	public static String getContextUrl(final HttpServletRequest r) {
        return getHostURL(r, new StringBuilder()).append(r.getContextPath()).toString();
	}

    public static String getFullURL(final HttpServletRequest r) {
        StringBuffer buffer= r.getRequestURL();
        String query= r.getQueryString();
        if (query!=null)
            buffer.append(query);
        return buffer.toString();
    }

    public static InputStream getWrappedInputStreamFromConnection(HttpURLConnection connection) {
        try {
            return getWrappedInputStream(connection.getInputStream(), "gzip".equalsIgnoreCase(connection.getContentEncoding()));
        } catch (IOException e) { throw Bark.unchecker(e); }
    }

    public static InputStream getWrappedErrorStreamFromConnection(HttpURLConnection connection) {
        return getWrappedInputStream(connection.getErrorStream(), "gzip".equalsIgnoreCase(connection.getContentEncoding()));
    }

    public static InputStream getWrappedInputStream(InputStream is, boolean gzip) {
        try {
            if (gzip) {
                return new BufferedInputStream(new GZIPInputStream(is));
            } else {
                return new BufferedInputStream(is);
            }
        } catch (IOException e) { throw Bark.unchecker(e); }
    }
}
